﻿using DX;
using DX.Behaviors;
using DX.Comm;
using DX.ViewModels;
using DX.Views;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Tools.Views
{
    public partial class MacOSMainForm : ViewForm, ILoadPluginsBehavior
    {
        public MacOSMainForm()
        {
            InitializeComponent();

        }
        protected override void OnAttachView(IView view, IViewModel viewModel)
        {
            base.OnAttachView(view, viewModel);
            toolStripSplitButton1.Command = ViewModel.TryCloseCommand;
        }
        protected override void OnDetachView(IView view, IViewModel viewModel)
        {
            base.OnDetachView(view, viewModel);
            timer1.Enabled = false;
            timer1.Dispose();
        }
        void ILoadPluginsBehavior.AddPlugins(PluginProfile pluginProfile, EventHandler click)
        {
            if (pluginProfile == null) return;
            PluginGroup pluginGroup = new PluginGroup();
            pluginGroup.Name = pluginProfile.Name;
            pluginGroup.Title = pluginProfile.Title;

            EventHandler itemClick = (sender, e) =>
            {
                if (this.ViewModel == null) return;
                var viewModelType = Type.GetType(pluginProfile.Type);
                var routFactory = this.ViewModel.Context.ServiceProvider.GetRequiredService<IFormRoutFactory>();
                if (pluginProfile.AllowMultipleCreation)
                {
                    routFactory.Show(viewModelType);
                }
                else
                {
                    routFactory.FindViewShow(viewModelType);
                }
                click?.Invoke(sender, e);

            };
            pluginGroup.ItemClick += itemClick;

            flowLayoutPanel1.Controls.Add(pluginGroup);

        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            labnowtime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }


    }
}
